﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;

namespace WindSim.Converter
{
    //SingleValueConverter
    public class IsConnectedToVisibleOrCollapsedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool bValue = (bool)value;
            if (!bValue)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility visibility = (Visibility)value;

            if (visibility == Visibility.Visible)
                return false;
            else
                return true;
        }
    }
    public class PowerStateToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is SimServiceReference.PowerState)
            {
                SimServiceReference.PowerState ps = (SimServiceReference.PowerState)value;
                if (ps == SimServiceReference.PowerState.On)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is bool)
            {
                bool b = (bool)value;
                if (b)
                {
                    return SimServiceReference.PowerState.On;
                }
                else
                {
                    return SimServiceReference.PowerState.Off;
                }
            }
            else
            {
                return SimServiceReference.PowerState.Off;
            }
        }
    }
    public class PowerStateToIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is SimServiceReference.PowerState)
            {
                SimServiceReference.PowerState ps = (SimServiceReference.PowerState)value;
                if (ps == SimServiceReference.PowerState.On)
                {
                    return 100;
                }
                else if(ps == SimServiceReference.PowerState.Off)
                {
                    return 0;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is int)
            {
                int b = (int)value;
                if (b == 100)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
    public class FunctionStateToFunctionButtonTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is SimServiceReference.FunctionState)
            {
                SimServiceReference.FunctionState fs = (SimServiceReference.FunctionState)value;
                if (fs == SimServiceReference.FunctionState.OK)
                {
                    return "Störung simulieren";
                }
                else
                {
                    return "Störung beheben"; ;
                }
            }
            else
            {
                return "Status Unbekannt";
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return SimServiceReference.FunctionState.OK;
        }

    }
    public class FunctionStateToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is SimServiceReference.FunctionState)
            {
                SimServiceReference.FunctionState fs = (SimServiceReference.FunctionState)value;
                if (fs == SimServiceReference.FunctionState.OK)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is bool)
            {
                bool b = (bool)value;
                if (b)
                {
                    return SimServiceReference.FunctionState.OK;
                }
                else
                {
                    return SimServiceReference.FunctionState.Error;
                }
            }
            else
            {
                return SimServiceReference.FunctionState.OK;
            }
        }
    }
    public class EnumToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            return value.Equals(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            if (value.Equals(false))
                return DependencyProperty.UnsetValue;
            else
                return parameter;
        }
    }
    public class DegreeToCompassDirectionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is double)
            {
                double dv = (double)value;
                int di = (int)dv;

                if(di == 0 || di == 360)
	            {
                    return "N";
	            }
                if (di >= 1 && di <= 89)
                {
                    return "NO";
                }
                else if (di == 90)
                {
                    return "O";
                }
                else if (di >= 91 && di <= 179)
                {
                    return "SO";
                }
                else if (di == 180)
                {
                    return "S";
                }
                else if (di >= 181 && di <= 269)
                {
                    return "SW";
                }
                else if (di == 270)
                {
                    return "W";
                }
                else if (di >= 271 && di <= 359)
                {
                    return "NW";
                }
                else
                {
                    return "N";
                }
            }
            else
            {
                return "N";
            }           
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class WindSpeedToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is double)
            {
                double dv = (double)value;

                return String.Format("{0} m/s", dv);
            }
            else
                return "0 m/s";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class RPMToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is double)
            {
                double dv = (double)value;

                return String.Format("{0} U/min", dv.ToString("0.00"));
            }
            else
                return "0 U/min";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class PowerToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is double)
            {
                double dv = (double)value;

                if (dv >= 0.0 && dv < 1000.0)
                {
                    return String.Format("{0} W", dv.ToString("0.00"));                    
                }
                else if (dv >= 1000.0 && dv < 1000000.0)
                {
                    dv = dv / 1000.0;
                    return String.Format("{0} kW", dv.ToString("0.00"));
                }
                else
                {
                    dv = dv / 1000000.0;
                    return String.Format("{0} MW", dv.ToString("0.00"));
                }
            }
            else
                return "0 W";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class PowerToDashboardValueConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is double)
            {
                double dv = (double)value;
                if (dv >= 0.0 && dv <= 3000000.0)
                {
                    dv = dv / 1000000.0;
                    return dv;
                }
                else
                {
                    return 0.0;
                }
            }
            else
            {
                return 0.0;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class DayTimeToAxisAngleRotation3DConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //<!--Tag   (12:00):   <AxisAngleRotation3D Axis="0.07,0.12,1" Angle="243"/>-->
            //<!--Nacht (00:00):   <AxisAngleRotation3D Axis="-0.5,0.8,0.25" Angle="211"/>-->
            //Tag -> Nacht: 720 Minuten

            int halfDayMinutes = 720;

            if (value != null && value is DateTime)
            {
                DateTime dt = (DateTime)value;

                int minutes = dt.Minute + (dt.Hour * 60);

                double deltaXAxisTagNacht = 0.0006944; //-
                double deltaYAxisTagNacht = 0.0011111; //+
                double deltaZAxisTagNacht = 0.0009722; //-
                double deltaAngleTagNacht = 0.0666666; //-

                double deltaXAxisNachtTag = 0.0006944; //+
                double deltaYAxisNachtTag = 0.0011111; //-
                double deltaZAxisNachtTag = 0.0009722; //+
                double deltaAngleNachtTag = 0.0666666; //+

                if (minutes >= 0 && minutes <= halfDayMinutes)
                {
                    Vector3D newVec = new Vector3D();
                    newVec.X = -0.4 + (minutes * deltaXAxisNachtTag);
                    newVec.Y = 0.8 - (minutes * deltaYAxisNachtTag);
                    newVec.Z = 0.3 + (minutes * deltaZAxisNachtTag);

                    double newAngle = 185 + (minutes * deltaAngleNachtTag);

                    return new AxisAngleRotation3D(newVec, newAngle);
                }
                else
                {
                    Vector3D newVec = new Vector3D();
                    newVec.X = 0.1 - ((minutes - halfDayMinutes) * deltaXAxisTagNacht);
                    newVec.Y = 0.0 + ((minutes - halfDayMinutes) * deltaYAxisTagNacht);
                    newVec.Z = 1.0 - ((minutes - halfDayMinutes) * deltaZAxisTagNacht);

                    double newAngle = 243 - ((minutes - halfDayMinutes) * deltaAngleTagNacht);

                    return new AxisAngleRotation3D(newVec, newAngle);
                }
            }
            else
            {
                return new AxisAngleRotation3D(new Vector3D(0.1,0.0,1.0), 243); 
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    //MultiValueConverter
    public class DisableEnableFunctionstateButtonConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values[1] != null && 
                values[2] is SimServiceReference.PowerState && 
                (SimServiceReference.PowerState)values[2] == SimServiceReference.PowerState.On &&
                values[3] is SimServiceReference.SimControlType &&
                (SimServiceReference.SimControlType)values[3] == SimServiceReference.SimControlType.Manual)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class DisableEnableControlsConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values[1] != null &&
                values[2] is SimServiceReference.SimControlType &&
                (SimServiceReference.SimControlType)values[2] == SimServiceReference.SimControlType.Manual)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class ShowRetryServiceSearchButtonConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values[1] != null && values[2] != null && values[1] is bool && values[2] is bool)
            {
                bool bv1 = (bool)values[1];
                bool bv2 = (bool)values[2];

                if (!bv1 && !bv2)
                {
                    return Visibility.Visible;
                }
                else
                    return Visibility.Hidden;
            }
            else
            {
                return Visibility.Hidden;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
